﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class ResourcesLoad : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //AssetBundle bundle=AssetBundle.LoadFromFile(Application.streamingAssetsPath+"/"+ "cube");
        //   GameObject cube = GameObject.Instantiate(bundle.LoadAsset<GameObject>("Cube")) ;
        // TestSerilize testSeriliz= SerilizeTest();
        //   XmlSerilize(testSeriliz);
        //   XmlDeSerilizeTest();
        //   CreateBinarySerilizeTest();
        //BinaryDeSerilize();
        ReadAssetSerilizeTest();
    }

    /// <summary>
    /// 读取asset 序列化
    /// </summary>
    void ReadAssetSerilizeTest()
    {
        //AssetSerilize asset = UnityEditor.AssetDatabase.LoadAssetAtPath<AssetSerilize>("Assets/TestAssets.asset");
        //Debug.Log(asset.Id+" name="+asset.Name);
        //for (int i = 0; i < asset.TestList.Count; i++)
        //{
        //    Debug.Log(asset.TestList[i]);
        //}
    }

    TestSerilize SerilizeTest()
    {
        TestSerilize testserilize = new TestSerilize();
        testserilize.Id = 1;
        testserilize.Name = "测试";
        testserilize.List = new List<int>();
        testserilize.List.Add(1);
        testserilize.List.Add(2);
        return testserilize;
    }

    /// <summary>
    /// XML 序列化
    /// </summary>
    /// <param name="testSerilize"></param>
    void XmlSerilize(TestSerilize testSerilize)
    {
        //打开文件流
        FileStream fileStream = new FileStream(Application.dataPath+"/"+"test.xml", FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
        //文件写入流
        StreamWriter sw = new StreamWriter(fileStream,System.Text.Encoding.UTF8);
        //序列化 传入要序列化的类型
        XmlSerializer xml = new XmlSerializer(testSerilize.GetType());
        xml.Serialize(sw,testSerilize);

        sw.Close();
        fileStream.Close();
    }


    void XmlDeSerilizeTest()
    {
        TestSerilize testSerilize = XmlDeSerilize();
        Debug.Log(testSerilize.Id+" name="+ testSerilize.Name);
    }


    /// <summary>
    /// XML 反序列化
    /// </summary>
    TestSerilize XmlDeSerilize()
    {
        //文件打开流
        FileStream fs = new FileStream(Application.dataPath+"/"+"test.xml",FileMode.Open,FileAccess.ReadWrite,FileShare.Read);
        XmlSerializer serializer = new XmlSerializer(typeof(TestSerilize));
        TestSerilize testSerilize=(TestSerilize) serializer.Deserialize(fs);
        fs.Close();
        return testSerilize;
    }



    /// <summary>
    ///创建二进制类
    /// </summary>
    void CreateBinarySerilizeTest()
    {
        TestSerilize testserilize = new TestSerilize();
        testserilize.Id = 1;
        testserilize.Name = "二进制测试";
        testserilize.List = new List<int>();
        testserilize.List.Add(1);
        testserilize.List.Add(2);
        BinarySerilize(testserilize);
    }

    /// <summary>
    /// 类数据转换成二进制文件
    /// </summary>
    void BinarySerilize(TestSerilize testSerilize)
    {
        FileStream fs = new FileStream(Application.dataPath+"/"+"test.bytes",FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
        BinaryFormatter bf = new BinaryFormatter();
        //序列化成二进制数据
        bf.Serialize(fs,testSerilize);
        fs.Close();
    }


    /// <summary>
    /// 反序列文件
    /// </summary>
    void BinaryDeSerilize()
    {
        //TextAsset textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/"+"test.bytes");
        ////读取二进制流
        //MemoryStream stream = new MemoryStream(textAsset.bytes);
        ////二进制反序列化
        //BinaryFormatter bf = new BinaryFormatter();
        //TestSerilize test= (TestSerilize)bf.Deserialize(stream);
        //Debug.Log("test"+ test.Id+" name="+test.Name);
    }

}
